package com.hsgene.common.util.result;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.hsgene.common.util.exception.ErrorCode;

import java.io.Serializable;

/**
 * 接口调用返回结果
 *
 * @author wangbing
 * @version 1.0, 2018/6/22
 */
public class ApiResult<T> implements Serializable {

    private static final long serialVersionUID = -1819003631683535598L;

    /**
     * 接口调用成功状态码
     */
    public final static int SUCCESS_CODE = 200;

    /**
     * 接口调用成功message消息
     */
    public final static String SUCCESS_MESSAGE = "success";

    /**
     * 接口调用成功desc消息
     */
    public final static String SUCCESS_DESC = "操作成功";

    /**
     * 状态码：200表示正常，其它表示错误码
     */
    private int code;

    /**
     * 成功／失败信息
     */
    private String message;

    /**
     * 错误描述
     */
    private String desc;

    /**
     * 业务数据
     */
    private T data;

    /**
     * 接口调用是否成功.
     *
     * @return true：成功
     */
    @JsonIgnore
    public boolean isSucc() {
        return this.code == SUCCESS_CODE;
    }

    /**
     * 获取完整错误信息
     *
     * @return 返回结果成功，则ErrorCode为null
     */
    @JsonIgnore
    public ErrorCode getError() {
        if (isSucc()) {
            return null;
        }

        return new ErrorCode() {
            @Override
            public int getCode() {
                return code;
            }

            @Override
            public String getMessage() {
                return message;
            }

            @Override
            public String getDescription() {
                return desc;
            }
        };
    }

    /**
     * 构建成功调用返回结果
     *
     * @return 返回结果
     */
    public static <T> ApiResult<T> succ() {
        return new ApiResult<T>();
    }

    /**
     * 构建成功调用返回结果
     *
     * @param data 业务数据
     * @return 返回结果
     */
    public static <T> ApiResult<T> succ(T data) {
        return new ApiResult<T>(data);
    }

    /**
     * 构建成功调用返回结果
     *
     * @param data        业务数据
     * @param description 成功提示
     * @return 返回结果
     */
    public static <T> ApiResult<T> succ(T data, String description) {
        return new ApiResult<T>(data, description);
    }


    /**
     * 构建错误调用返回结果
     *
     * @param error 错误信息
     * @return 返回结果
     */
    public static <T> ApiResult<T> fail(ErrorCode error) {
        return new ApiResult<T>(error);
    }

    /**
     * 构建错误调用返回结果
     *
     * @param error       错误信息
     * @param description 错误提示
     * @return 返回结果
     */
    public static <T> ApiResult<T> fail(ErrorCode error, String description) {
        return new ApiResult<T>(error, description);
    }

    /**
     * 构建错误调用返回结果
     *
     * @param error 错误信息
     * @param msg   信息
     * @return 返回结果
     */
    public static <T> ApiResult<T> failFormatTip(ErrorCode error, String... msg) {
        String description = String.format(error.getDescription(), msg);
        return new ApiResult<T>(error, description);
    }

    /**
     * 构建错误调用返回结果
     *
     * @param error 错误信息
     * @param data  业务数据
     * @return 返回结果
     */
    public static <T> ApiResult<T> fail(ErrorCode error, T data) {
        return new ApiResult<T>(error, data);
    }

    /**
     * 构建错误调用返回结果
     *
     * @param error       错误信息
     * @param description 错误提示
     * @param data        业务数据
     * @return 返回结果
     */
    public static <T> ApiResult<T> fail(ErrorCode error, String description, T data) {
        return new ApiResult<T>(error, description, data);
    }

    public ApiResult() {
        this.code = SUCCESS_CODE;
        this.message = SUCCESS_MESSAGE;
        this.desc = SUCCESS_DESC;
    }

    public ApiResult(int code, String message, String desc, T data) {
        this.code = code;
        this.message = message;
        this.desc = desc;
        this.data = data;
    }

    public ApiResult(T data) {
        this.code = SUCCESS_CODE;
        this.message = SUCCESS_MESSAGE;
        this.desc = SUCCESS_DESC;
        this.data = data;
    }

    public ApiResult(T data, String description) {
        this.data = data;
        this.message = SUCCESS_MESSAGE;
        this.code = SUCCESS_CODE;
        this.desc = description;
    }

    public ApiResult(ErrorCode error) {
        this.code = error.getCode();
        this.message = error.getMessage();
        this.desc = error.getDescription();
    }

    public ApiResult(ErrorCode error, String description) {
        this.code = error.getCode();
        this.message = error.getMessage();
        this.desc = description;
    }

    public ApiResult(ErrorCode error, T data) {
        this.code = error.getCode();
        this.message = error.getMessage();
        this.desc = error.getDescription();
        this.data = data;
    }

    public ApiResult(ErrorCode error, String description, T data) {
        this.code = error.getCode();
        this.message = error.getMessage();
        this.desc = description;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
